package listclassfields;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ListClassFields {

    public static void main(String[] args) throws IllegalAccessException {
        Set<String> allKindSet = new HashSet<>();
        listDictKind(PublicConstant.DictKind.class, allKindSet);
        // listDictKind(DictKind.class, allKindSet);
        insertDb(allKindSet);
    }

    private static void listDictKind(Class<?> dictKindClass, Set<String> allKindSet) throws IllegalAccessException {
        Field[] declaredFields = dictKindClass.getDeclaredFields();
        for (Field field : declaredFields) {
            Object staticFieldVal = field.get(null);// 静态字段值
            System.out.println(field.getType() + "  " + field.getName() + "  " + staticFieldVal);
            allKindSet.add(String.valueOf(staticFieldVal));
        }
    }

    private static void insertDb(Set<String> allKindSet) {
        // String url = "jdbc:mysql://localhost:3306/dxy2?generateSimpleParameterMetadata=true&serverTimezone=Asia/Shanghai&useSSL=false";
        // String username = "root";
        // String password = "root";

        // String url = "jdbc:mysql://172.25.22.250:3306/pd_dicmanage?generateSimpleParameterMetadata=true&serverTimezone=Asia/Shanghai&useSSL=false";
        // String username = "videoweb";
        // String password = "suntek";

        String url = "jdbc:mysql://172.25.22.26:3306/pd_dicmanage?generateSimpleParameterMetadata=true&serverTimezone=Asia/Shanghai&useSSL=false";
        String username = "videoweb";
        String password = "suntek";

        List<String> values = allKindSet.stream().map(s -> "(" + s + ")").collect(Collectors.toList());
        String insertSql = "insert into dictkind(kind) values " + StrUtil.join(",",values) + ";";

        try(Connection conn = DriverManager.getConnection(url, username, password);
            Statement stat = conn.createStatement();) {
            int row = stat.executeUpdate(insertSql);
            System.out.println("row: " + row);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
